package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.List;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/3 16:03
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_GET_CARD, desc = "水浒叶子牌 抽卡")
public class ShuiHuYeZiCardGetCardCmd extends AbstractCommand {

    int activityId;
    /**
     *  0单次 1五次
     */
    int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiCardGetCardReqMsg reqMsg = ShuiHuYeZiCardProto.ShuiHuYeZiCardGetCardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = ShuiHuYeZiCardMgr.canEntryActivity(player,activityId);
        if(ret != 0){
            return;
        }
        int consumeNum = 0;
        if(type == 0){
            // 单次
            consumeNum = ShuiHuYeZiCardMgr.getConfig().oneConsume;
        }else {
            // 五次
            consumeNum = ShuiHuYeZiCardMgr.getConfig().fiveConsume;
        }
        // 判断道具是否足够
        String consumeProperty = PropertyHelper.parseGoodsToString(ShuiHuYeZiCardMgr.getConfig().SHUIHUYEZICARD_GOLD_LEAF_ID, BigInteger.valueOf(consumeNum));
        Property consume = PropertyHelper.parseStringToProperty(consumeProperty);
        if(!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.ShuiHuYeZiCardActivity,eLogMoneyType.ShuiHuYeZiCardActivityGetCardConsume)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 随机
        ShuiHuYeZiCardProto.ShuiHuYeZiCardGetCardRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiCardGetCardRespMsg.newBuilder();
        List<ShuiHuYeZiCardProto.ShuiHuYeZiCardGetCardResult> resultList = ShuiHuYeZiCardMgr.randomCard(player,type);
        ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
        player.notifyListener(eGamePlayerEventType.ShuiHuYeZiCardTotalCardLevel.getValue(),shuiHuYeZiCardModule.getCardTotalLevel());
        player.notifyListener(eGamePlayerEventType.ShuiHuYeZiCardNumberOfGoldCards.getValue(),shuiHuYeZiCardModule.getGoldCardNum());
        // 同步玩家数据
        shuiHuYeZiCardModule.synUserData();
        resp.addAllResultList(resultList);
        resp.setRet(0);
        player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_GET_CARD,resp);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ShuiHuYeZiCardProto.ShuiHuYeZiCardGetCardRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiCardGetCardRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_GET_CARD,resp);
        }
    }
}
